package com.lw.lock;

import java.util.concurrent.TimeUnit;

/**
 * @author LW
 * @2020/8/22 11:16
 */
public class DeadLockDemo {

    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";

        MyThread myThread = new MyThread(lockA,lockB);
        new Thread(new MyThread(lockA,lockB),"t1").start();
        new Thread(new MyThread(lockA,lockB),"t2").start();

    }

}

class MyThread implements Runnable{

    private String lockA;
    private String lockB;

    public MyThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+"lock:"+lockA+"==>get lockB"+lockB);
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+"lock:"+lockB+"==>get lockA"+lockA);
            }
        }
    }
}